package com.jxinternet.platform.services.service.impl;

import com.jxinternet.platform.services.entity.domian.Message;
import com.jxinternet.platform.services.entity.domian.User;
import com.jxinternet.platform.services.entity.domian.Wealth;
import com.jxinternet.platform.services.entity.protobuf.LoginProtos.LoginRequest;
import com.jxinternet.platform.services.entity.protobuf.LoginProtos.LoginResponse;
import com.jxinternet.platform.services.protocol.BaseProtocol;
import com.jxinternet.platform.services.service.LoginService;
import com.jxinternet.platform.services.service.impl.steps.LoginSteps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by gold-jx on 2017/7/18.
 */
@Slf4j
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private LoginSteps loginSteps;

    private Message message;


    @Override
    public LoginResponse Login(LoginRequest loginRequest, String loginIp) {
        LoginResponse.Builder builder = LoginResponse.newBuilder();
        builder.setToken("");
        builder.setUserName("");
        //log.info("开始登录：" + "LoginRequest=[" + loginRequest + "]");
        try {
            //1.验证请求
            message = loginSteps.step1_validateRequest(loginRequest);
            if (message != null && message.getCode() != null) {
                builder.setResultStatus(message.getCode());
                builder.setResultDescription(message.getRes());
                return builder.build();
            }

            //2.验证用户
            String userName = loginRequest.getUserName();
            String password = loginRequest.getPassword();
            message = loginSteps.step2_validateUser(userName);

            if (message != null && message.getCode() != null) {
                builder.setResultStatus(message.getCode());
                builder.setResultDescription(message.getRes());
                return builder.build();
            }

            User user = (User) message.getData();

            //3.验证密码
            message = loginSteps.step3_validatePassword(user, password);

            if (message != null && message.getCode() != null) {
                builder.setResultStatus(message.getCode());
                builder.setResultDescription(message.getRes());
                return builder.build();
            }

            //4.验证财富
            message = loginSteps.step4_validateWealth(userName);

            if (message != null && message.getCode() != null) {
                builder.setResultStatus(message.getCode());
                builder.setResultDescription(message.getRes());
                return builder.build();
            }
            Wealth wealth = (Wealth) message.getData();

            //5.生成token
            message = loginSteps.step5_createToken(userName);

            if (message != null && message.getCode() != null) {
                builder.setResultStatus(message.getCode());
                builder.setResultDescription(message.getRes());
                return builder.build();
            }
            String token = (String) message.getData();

            //6.生成response
            builder.setToken(token)
                    .setUserName(user.getUserName())
                    .setResultStatus(BaseProtocol.Data_Succeed_Code)
                    .setResultDescription(BaseProtocol.Data_Succeed_Des);

            //log.info("登录成功");

            //7.保存日志
            message = loginSteps.step6_saveLog(userName, loginIp);

            if (message != null && message.getCode() != null) {
                builder.setResultStatus(message.getCode());
                builder.setResultDescription(message.getRes());
                return builder.build();
            }
            return builder.build();
        } catch (Exception e) {
            builder.setResultStatus(BaseProtocol.Login_InvalidExcption_Code);
            builder.setResultDescription(BaseProtocol.Login_InvalidExcption_Des);
            //log.error("登录失败", e);
            return builder.build();
        }
    }
}
